import { lastValueFrom } from "rxjs";
import { Injectable } from "@nestjs/common";

import {
  Media,
  RestDto,
  ENUM_APP,
  MediaGroup,
  findOneMediaRpcDto,
  MediaRpcFindOnePath,
  MediaRpcFindManyPath,
  insertOneMediaRpcDto,
  MediaRpcInsertOnePath,
  MediaRpcInterfaceWithDerive,
  MediaRpcGetHighestGroupSyncPath,
  PostRes,
  findManyMediaRpcDto,
  PageResult,
} from "qqlx-core";

import { BusRpc } from "./app.droplet-bus";
import { RpcWaiting } from "./rpc.response";
import { RpcConnection } from "./com.connection";

@Injectable()
export class MediaRpc extends RpcConnection implements MediaRpcInterfaceWithDerive<RestDto> {
  constructor(readonly BusRpc: BusRpc) {
    super(BusRpc, ENUM_APP.POND_MEDIA);
  }

  @RpcWaiting(MediaRpcGetHighestGroupSyncPath)
  getHighestGroupSync(dto: RestDto) {
    const ob = this.CLIENT.send(MediaRpcGetHighestGroupSyncPath, dto);
    return lastValueFrom<{ group: MediaGroup }>(ob);
  }

  @RpcWaiting(MediaRpcFindOnePath)
  findOne(dto: RestDto & findOneMediaRpcDto) {
    const ob = this.CLIENT.send(MediaRpcFindOnePath, dto);
    return lastValueFrom<Media>(ob);
  }

  @RpcWaiting(MediaRpcFindManyPath)
  findMany(dto: RestDto & findManyMediaRpcDto) {
    const ob = this.CLIENT.send(MediaRpcFindManyPath, dto);
    return lastValueFrom<PageResult<Media>>(ob);
  }

  @RpcWaiting(MediaRpcInsertOnePath)
  insertOne(dto: RestDto & insertOneMediaRpcDto) {
    const ob = this.CLIENT.send(MediaRpcInsertOnePath, dto);
    return lastValueFrom<PostRes<Media>>(ob);
  }
}
